<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\erp\logic;
use think\facade\Hook;
use think\DB;
use util\Tree;
use think\facade\Debug;
use think\facade\Cache;
/**
 * 收货地址管理
 * @package app\erp\model
 */
class Receivinglogic{

    /**
     * 首页
     * @author  shiqiang
     * @return array_out(code, message, data)
     */
    public function index($user_id){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
				'user_id' => $user_id,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
			$list =  model('UcMemberReceivingAddress', 'model') -> where('uid', $user_id) -> order('is_common desc,create_time desc')->select();//获取列表
			$rel_list = [];
            foreach($list as $key => $value){
				$rel_list[$key]['id'] 			= (int)$value['id'];
				$rel_list[$key]['userTitle'] 	= (string)$value['user_title'];
				$rel_list[$key]['userCity'] 	= (string)$value['user_city'];
				$rel_list[$key]['userAddress'] 	= (string)$value['user_address'];
				$rel_list[$key]['userMobile'] 	= (string)$value['user_mobile'];
				$rel_list[$key]['createTime'] 	= (int)$value['create_time'];
				$rel_list[$key]['isCommon'] 	= (int)$value['is_common'];//是否常用 1常用
			}
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', $rel_list);
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 添加
     * @param user_title 	        string  收件人
     * @param user_address 		    string  收货地址
     * @param user_mobile 		    int		电话
     * @param is_common 		    int     是否常用
     * @author shiqiang
     * @return array_out(code, message, data)
     */
    public function add($user_id, $user_title, $user_city, $user_address, $user_mobile, $is_common){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
				'user_id' 		=> $user_id,
				'user_title'    => $user_title,
				'user_city'    	=> $user_city,
				'user_address' 	=> $user_address,
				'user_mobile'   => $user_mobile,
				'is_common' 	=> $is_common,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
			if($is_common == 1){ // 常用
				$map = [];
				$map['uid'] = $user_id;
				
				$data = [];
				$data['is_common'] = 0;
				
				$rel = model('UcMemberReceivingAddress') -> update($data, $map);
				
				if(!$rel){
					DB::rollback();
					return array_out(-1005, 'error1');//编辑失败
				}
			}
			
			$rel = model('UcMemberReceivingAddress') -> addData($user_id, $user_title, $user_city, $user_address, $user_mobile, $is_common);//添加
            if($rel['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');//添加失败 返回-1005,error
            }
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success',$rel['data']);
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 编辑
     * @param id 	                string  id
     * @param user_title 	        string  收件人
     * @param user_address 		    string  收货地址
     * @param user_mobile 		    int		电话
     * @param is_common 		    int     是否常用
     * @author shiqiang
     * @return array_out(code, message, data)
     */
    public function edit($user_id, $id, $user_title, $user_city, $user_address, $user_mobile, $is_common){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
				'user_id' 		=> $user_id,
				'id' 			=> $id,
                'user_title'    => $user_title,
                'user_city'    	=> $user_city,
				'user_address' 	=> $user_address,
				'user_mobile'   => $user_mobile,
				'is_common' 	=> $is_common,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
			if($is_common == 1){ // 常用
				$map = [];
				$map['uid'] = $user_id;
				$data = [];
				$data['is_common'] 		= 0;
				
				$rel = model('UcMemberReceivingAddress') -> update($data, $map);
				
				if(!$rel){
					DB::rollback();
					return array_out(-1005, 'error1');//编辑失败
				}
			}
			
			$rel = model('UcMemberReceivingAddress') -> editData($id, $user_title, $user_city, $user_address, $user_mobile, $is_common);
            if($rel['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');//编辑失败 返回-1005,error
            }
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 删除
     * @param int $id
     * @author shiqiang
     * @return bool
     */
    public function delete($user_id, $id){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
				'user_id' 	=> $user_id,
				'id' 		=> $id,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
			$rel = model('UcMemberReceivingAddress') -> deleteData($id);
			if($rel['code'] < 0){
				return array_out(-1005, 'error');//删除失败 返回-1005,error
			}
			
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }
}